package org.example.springbootdemo02.controller;


import org.example.springbootdemo02.dto.Page;
import org.example.springbootdemo02.pojo.PageResult;
import org.example.springbootdemo02.pojo.Product;
import org.example.springbootdemo02.pojo.Result;
import org.example.springbootdemo02.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping ("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/getAll")
    public Result getAll() {
        List<Product> all = productService.getAll();
        return Result.success(all);
    }

    @RequestMapping("/getByCondition")
    public Result getByCondition(@RequestBody Map<String, Object> params) {
        Object o = params.get("name");
        System.out.println(o);
        List<Product> all = productService.getByCondition(params);
        return Result.success(all);
    }

    @RequestMapping("/deleteByCondition")
    public Result deleteByCondition(@RequestBody Map<String, Object> params) {
        int i = productService.deleteByCondition(params);
        return Result.success(i);
    }

    @RequestMapping("/getById")
    public Result getById(Integer id) {
        Product byId = productService.getById(id);
        return Result.success(byId);
    }

    @RequestMapping("/insert")
    public Result insert(Product product) {
        int insert = productService.insert(product);
        return Result.success(insert);
    }

    @RequestMapping("/batchInsert")
    public Result batchInsert(@RequestBody List<Product> productList) {
        int i = productService.batchInsert(productList);
        return Result.success(i);
    }

    @RequestMapping("/update")
    public Result update(Product product) {
        int update = productService.update(product);
        return Result.success(update);
    }

    @RequestMapping("/dynamicUpdate")
    public Result dynamicUpdate(@RequestBody Map<String, Object> params) {
        int i = productService.dynamicUpdate(params);
        return Result.success(i);
    }
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        int i = productService.delete(id);
        return Result.success(i);
    }

    @RequestMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        int i = productService.batchDelete(ids);
        return Result.success(i);
    }
    @RequestMapping ("/page")
    public Result getByPage(Page<Product> page) {
        PageResult<Product> pageResult = productService.getByPage(page);
        return Result.success(pageResult);
    }
}
